<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- 
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License. 
-->
<html>
<head>
    <link type="text/css" rel="stylesheet" href="https://struts.apache.org/css/default.css">
    <style type="text/css">
        .dp-highlighter {
            width:95% !important;
        }
    </style>
    <style type="text/css">
        .footer {
            background-image:      url('https://cwiki.apache.org/confluence/images/border/border_bottom.gif');
            background-repeat:     repeat-x;
            background-position:   left top;
            padding-top:           4px;
            color:                 #666;
        }
    </style>
    <link href='https://struts.apache.org/highlighter/style/shCoreStruts.css' rel='stylesheet' type='text/css' />
    <link href='https://struts.apache.org/highlighter/style/shThemeStruts.css' rel='stylesheet' type='text/css' />
    <script src='https://struts.apache.org/highlighter/js/shCore.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushPlain.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushXml.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushJava.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushJScript.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushGroovy.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushBash.js' type='text/javascript'></script>
    <script type="text/javascript">
        SyntaxHighlighter.defaults['toolbar'] = false;
        SyntaxHighlighter.all();
    </script>
    <script type="text/javascript" language="javascript">
        var hide = null;
        var show = null;
        var children = null;

        function init() {
            /* Search form initialization */
            var form = document.forms['search'];
            if (form != null) {
                form.elements['domains'].value = location.hostname;
                form.elements['sitesearch'].value = location.hostname;
            }

            /* Children initialization */
            hide = document.getElementById('hide');
            show = document.getElementById('show');
            children = document.all != null ?
                    document.all['children'] :
                    document.getElementById('children');
            if (children != null) {
                children.style.display = 'none';
                show.style.display = 'inline';
                hide.style.display = 'none';
            }
        }

        function showChildren() {
            children.style.display = 'block';
            show.style.display = 'none';
            hide.style.display = 'inline';
        }

        function hideChildren() {
            children.style.display = 'none';
            show.style.display = 'inline';
            hide.style.display = 'none';
        }
    </script>
    <title>CDI Plugin</title>
</head>
<body onload="init()">
<table border="0" cellpadding="2" cellspacing="0" width="100%">
    <tr class="topBar">
        <td align="left" valign="middle" class="topBarDiv" align="left" nowrap>
            &nbsp;<a href="home.html">Home</a>&nbsp;&gt;&nbsp;<a href="guides.html">Guides</a>&nbsp;&gt;&nbsp;<a href="plugin-developers-guide.html">Plugin Developers Guide</a>&nbsp;&gt;&nbsp;<a href="cdi-plugin.html">CDI Plugin</a>
        </td>
        <td align="right" valign="middle" nowrap>
            <form name="search" action="https://www.google.com/search" method="get">
                <input type="hidden" name="ie" value="UTF-8" />
                <input type="hidden" name="oe" value="UTF-8" />
                <input type="hidden" name="domains" value="" />
                <input type="hidden" name="sitesearch" value="" />
                <input type="text" name="q" maxlength="255" value="" />
                <input type="submit" name="btnG" value="Google Search" />
            </form>
        </td>
    </tr>
</table>

<div id="PageContent">
    <div class="pageheader" style="padding: 6px 0px 0px 0px;">
        <!-- We'll enable this once we figure out how to access (and save) the logo resource -->
        <!--img src="/wiki/images/confluence_logo.gif" style="float: left; margin: 4px 4px 4px 10px;" border="0"-->
        <div style="margin: 0px 10px 0px 10px" class="smalltext">Apache Struts 2 Documentation</div>
        <div style="margin: 0px 10px 8px 10px"  class="pagetitle">CDI Plugin</div>

        <div class="greynavbar" align="right" style="padding: 2px 10px; margin: 0px;">
            <a href="https://cwiki.apache.org/confluence/pages/editpage.action?pageId=27842284">
                <img src="https://cwiki.apache.org/confluence/images/icons/notep_16.gif"
                     height="16" width="16" border="0" align="absmiddle" title="Edit Page"></a>
            <a href="https://cwiki.apache.org/confluence/pages/editpage.action?pageId=27842284">Edit Page</a>
            &nbsp;
            <a href="https://cwiki.apache.org/confluence/pages/listpages.action?key=WW">
                <img src="https://cwiki.apache.org/confluence/images/icons/browse_space.gif"
                     height="16" width="16" border="0" align="absmiddle" title="Browse Space"></a>
            <a href="https://cwiki.apache.org/confluence/pages/listpages.action?key=WW">Browse Space</a>
            &nbsp;
            <a href="https://cwiki.apache.org/confluence/pages/createpage.action?spaceKey=WW&fromPageId=27842284">
                <img src="https://cwiki.apache.org/confluence/images/icons/add_page_16.gif"
                     height="16" width="16" border="0" align="absmiddle" title="Add Page"></a>
            <a href="https://cwiki.apache.org/confluence/pages/createpage.action?spaceKey=WW&fromPageId=27842284">Add Page</a>
            &nbsp;
            <a href="https://cwiki.apache.org/confluence/pages/createblogpost.action?spaceKey=WW&fromPageId=27842284">
                <img src="https://cwiki.apache.org/confluence/images/icons/add_blogentry_16.gif"
                     height="16" width="16" border="0" align="absmiddle" title="Add News"></a>
            <a href="https://cwiki.apache.org/confluence/pages/createblogpost.action?spaceKey=WW&fromPageId=27842284">Add News</a>
        </div>
    </div>

    <div class="pagecontent">
        <div class="wiki-content">
            <div id="ConfluenceContent"><style type="text/css">/*<![CDATA[*/
div.rbtoc1440489369636 {padding: 0px;}
div.rbtoc1440489369636 ul {list-style: disc;margin-left: 0px;}
div.rbtoc1440489369636 li {margin-left: 0px;padding-left: 0px;}

/*]]>*/</style><div class="toc-macro rbtoc1440489369636">
<ul class="toc-indentation"><li><a shape="rect" href="#CDIPlugin-Overview">Overview</a></li><li><a shape="rect" href="#CDIPlugin-Setup">Setup</a>
<ul class="toc-indentation"><li><a shape="rect" href="#CDIPlugin-MavenSetup">Maven Setup</a></li><li><a shape="rect" href="#CDIPlugin-ManualIntegration">Manual Integration</a></li><li><a shape="rect" href="#CDIPlugin-Configuration">Configuration</a></li></ul>
</li><li><a shape="rect" href="#CDIPlugin-Usage">Usage</a>
<ul class="toc-indentation"><li><a shape="rect" href="#CDIPlugin-ExampleUsage">Example Usage</a></li></ul>
</li></ul>
</div>

<h1 id="CDIPlugin-Overview">Overview</h1>

<p>CDI - short for Contexts and Dependency Injection - is the new standard for Java EE (though not limited to it) dependency injection frameworks. It was introduced as <a shape="rect" class="external-link" href="http://jcp.org/en/jsr/summary?id=299" rel="nofollow">JSR 299</a>, being a part of the <a shape="rect" class="external-link" href="http://jcp.org/en/jsr/detail?id=316" rel="nofollow">Java EE 6</a> umbrella specification. It is also important to note that it builds on top of JSR 330 (Dependency Injection for Java SE), often referenced as <a shape="rect" class="external-link" href="http://jcp.org/en/jsr/summary?id=330" rel="nofollow">@Inject</a>.</p>

<p>It can be used as standalone solution packed with your web applications to run in lightweight servlet containers such as Apache Tomcat or Jetty, utilizing standalone CDI implementations such as <a shape="rect" class="external-link" href="http://seamframework.org/Weld" rel="nofollow">JBoss Weld</a> (the JSR 299 reference implementation) or <a shape="rect" class="external-link" href="http://openwebbeans.apache.org/">Apache OpenWebBeans</a>. On the other hand, each JEE 6 compliant application server such as <a shape="rect" class="external-link" href="http://glassfish.java.net/" rel="nofollow">Glassfish</a>, <a shape="rect" class="external-link" href="http://www.jboss.org/jbossas" rel="nofollow">JBoss AS</a> or <a shape="rect" class="external-link" href="http://openejb.apache.org/apache-tomee.html">Apache TomEE</a> is required to have a CDI container on board, making it easy for developers to pack lightweight web or enterprise archives while being able to use a compelling dependency injection feature set.</p>

<p>With the Struts 2 CDI plugin, your application is open to be embedded into this modern, comprehensive and type safe framework and take full use of it.</p>

<h1 id="CDIPlugin-Setup">Setup</h1>

<p>As with all Struts 2 plugins, usage of the CDI plugin is as easy as delivering the right jars with your web application. The plugin then registers itself as the standard ObjectFactory for your Struts 2 application.</p>

<div class="confluence-information-macro confluence-information-macro-information"><p class="title">Don't mess up Object Factory Providers</p><span class="aui-icon aui-icon-small aui-iconfont-info confluence-information-macro-icon"></span><div class="confluence-information-macro-body">
<p>Struts 2 delivers integration into all relevant dependency injection containers via plugins, such as the <a shape="rect" href="spring-plugin.html">Spring Plugin</a> or the externally provided <a shape="rect" class="external-link" href="http://code.google.com/p/google-guice/wiki/Struts2Integration" rel="nofollow">Guice Struts 2 Plugin</a>. Be aware that the central feature of all those, as well as the CDI plugin, is to register itself as Struts 2 Object factory. Usually you will want to use <strong>exactly one of those plugins</strong> in your application, since there will only be <strong>one ObjectFactory</strong> implementation for Struts 2 to chose. If you pack more than one of those plugins, the factory to be chosen will be undetermined. One will win in the end, but who knows which ...</p></div></div>

<h2 id="CDIPlugin-MavenSetup">Maven Setup</h2>

<p>The most easy and recommended way use is to simply add a maven dependency for it, along with a dependency to the CDI API for development time usage:</p>

<div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>pom.xml</b></div><div class="codeContent panelContent pdl">
<pre class="brush: xml; gutter: false; theme: Default" style="font-size:12px;">
&lt;dependencies&gt;
    ...
    &lt;dependency&gt;
        &lt;groupId&gt;org.apache.struts&lt;/groupId&gt;
        &lt;artifactId&gt;struts2-cdi-plugin&lt;/artifactId&gt;
        &lt;version&gt;${struts2.version}&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;javax.enterprise&lt;/groupId&gt;
        &lt;artifactId&gt;cdi-api&lt;/artifactId&gt;
        &lt;version&gt;1.0-SP1&lt;/version&gt;
        &lt;scope&gt;provided&lt;/scope&gt;
    &lt;/dependency&gt;
    ...
&lt;/dependencies&gt;
</pre>
</div></div>

<p>whereas </p>
<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">${struts2.version}</pre>
</div></div>
<p> should be set either as a property in your pom (recommended) or substituted by a concrete version information, such as 2.3.2 by the time of writing of this document. If you want to use CDI without an application server providing it, you may also want to add a dependency to a CDI implementation of your choice, for example Weld:</p>

<div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>pom.xml with CDI implementation</b></div><div class="codeContent panelContent pdl">
<pre class="brush: xml; gutter: false; theme: Default" style="font-size:12px;">
&lt;dependencies&gt;
    ...
    &lt;dependency&gt;
        &lt;groupId&gt;org.apache.struts&lt;/groupId&gt;
        &lt;artifactId&gt;struts2-cdi-plugin&lt;/artifactId&gt;
        &lt;version&gt;${struts2.version}&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;javax.enterprise&lt;/groupId&gt;
        &lt;artifactId&gt;cdi-api&lt;/artifactId&gt;
        &lt;version&gt;1.0-SP1&lt;/version&gt;
        &lt;scope&gt;provided&lt;/scope&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.jboss.weld&lt;/groupId&gt;
        &lt;artifactId&gt;weld-core&lt;/artifactId&gt;
        &lt;version&gt;1.0.1-Final&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.jboss.weld&lt;/groupId&gt;
        &lt;artifactId&gt;weld-se&lt;/artifactId&gt;
        &lt;version&gt;1.0.1-Final&lt;/version&gt;
    &lt;/dependency&gt;
    ...
&lt;/dependencies&gt;
</pre>
</div></div>

<h2 id="CDIPlugin-ManualIntegration">Manual Integration</h2>

<p>Ensure that the struts2-cdi-plugin jar matching your used Struts 2 framework version - it is part of the <a shape="rect" class="external-link" href="http://struts.apache.org/download.cgi">Struts 2 distribution</a> - is packed within the application archive that will be delivered. If a CDI implementation is provided within your container, no other dependencies are needed. If the latter is not the case, you may want to install a CDI container into your container (as commmon installation or packed with your app) according to the installation instructions of the CDI provider of choice.</p>

<p>For IDE integration, be sure to add both the struts2-cdi-plugin-VERSION.jar as well as the cdi-api.jar to your project setup. Remember, the API jar should not be delivered with your application archive since you will have the API provided by the CDI container your application will be running with.</p>

<h2 id="CDIPlugin-Configuration">Configuration</h2>

<p>Not much to see here. The CDI specification requires the CDI container to be exposed via JNDI context, and the CDI plugin will pick it up just there. Nevertheless, since there are different JNDI namespaces in use, the plugin will try to automatically find the right one among known references, that is</p>

<ul><li>java:comp/BeanManager as the CDI docs point out</li><li>java:app/BeanManager as the Weld docs point out</li><li>java:comp/env/BeanManager for containers limited to that namespace, such as tomcat</li></ul>


<p>If your container of choice uses other naming references, the plugin has a single configuration option to point to the right name. Just add a constant like this to your struts.xml to be on the happy path again:</p>

<div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>struts.xml</b></div><div class="codeContent panelContent pdl">
<pre class="brush: xml; gutter: false; theme: Default" style="font-size:12px;">
    &lt;constant name="struts.objectFactory.cdi.jndiKey" value="java:comp/some/weird/BeanManagerReference" /&gt;
</pre>
</div></div>

<h1 id="CDIPlugin-Usage">Usage</h1>

<p>CDI has an extremely rich feature set, and this section is not intended as a replacement for the CDI reference documentation or the excellent documentation found on the <a shape="rect" class="external-link" href="http://seamframework.org/Weld" rel="nofollow">JBoss Weld</a> homepage. The whole Struts 2 CDI integration works just as you would think it works, using JSR-299 and JSR-330 Annotations in your Struts 2 actions or interceptors. Be aware of the following, though:</p>

<div class="confluence-information-macro confluence-information-macro-note"><p class="title">Remember to provide beans.xml</p><span class="aui-icon aui-icon-small aui-iconfont-warning confluence-information-macro-icon"></span><div class="confluence-information-macro-body">
<p>CDI is designed to act in a type safe and modern way, thus using annotations for all aspects of the framework. Nevertheless, you are allowed to eternalize configuration into a XML file called beans.xml to be placed in your WEB-INF directory. Even if you don't intend to use such external configuration, it is an <strong>inevitable requirement to provide at least an empty beans.xml in WEB-INF !</strong></p></div></div>

<h2 id="CDIPlugin-ExampleUsage">Example Usage</h2>

<p>Implement your Struts 2 actions or interceptors just as usual, enriched with CDI and @Inject annotations:</p>

<div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>NumberGuess.java</b></div><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
package org.apache.struts2.example.cdi;

import com.opensymphony.xwork2.ActionSupport;
import javax.inject.Inject;

public class NumberGuess extends ActionSupport {

    @Inject
    Game game;

    public Game getGame() {
        return game;
    }

    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }

    public String guess() throws Exception {
        final String errorMessage = game.check();
        addActionError(errorMessage);

        if (Game.CORRECT.equals(errorMessage)) {
            game.reset();
        } else if (game.getRemainingGuesses() == 0) {
            addActionError("The correct guess was " + game.getGuess() + ". Game is reset.");
            game.reset();
        }


        return SUCCESS;
    }


}
</pre>
</div></div>

<p>See the @Inject annotation from javax.inject.Inject? While you could use all the other nice stuff, we usually would - following the separation of concerns principle - use @Inject most of the time only in our actions, keeping the more fancy stuff in the business layer as shown below. Using @Inject brings us to the most common pitfall when using Struts 2 together with JSR-330 / CDI:</p>

<div class="confluence-information-macro confluence-information-macro-information"><p class="title">Use the right @Inject</p><span class="aui-icon aui-icon-small aui-iconfont-info confluence-information-macro-icon"></span><div class="confluence-information-macro-body">
<p>Struts 2 and it's core component XWork use it's own internal dependency injection container. Interestingly, you could name it JSR-330's grandma, since it is an early pre-release version of <a shape="rect" class="external-link" href="http://code.google.com/p/google-guice/" rel="nofollow">Google Guice</a> once developed by <a shape="rect" class="external-link" href="http://blog.crazybob.org/" rel="nofollow">Crazybob Lee</a> - the same Bob Lee that, together with SpringSource's Rod Johnson, lead the JSR-330 specification.</p>

<p>That said, you will find the @Inject annotation both as <code>com.opensymphony.xwork2.inject.Inject</code> and <code>javax.inject.Inject</code>. <strong>Don't mix up those two - <code>javax.inject.Inject</code> is the one you want to use with your Struts 2 CDI plugin and CDI integration in general!</strong> While you could use Struts' internal annotation as well, the effect may be strange to undefined - so check your imports!</p></div></div>

<p>Now that you are aware of that, here is the rest of the inevitable NumberGuess CDI example in Struts 2 flavour. Add a JSP view similar to this:</p>
<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: html; gutter: false; theme: Default" style="font-size:12px;">
&lt;%@ page contentType="text/html; charset=UTF-8" %&gt;
&lt;%@ taglib prefix="s" uri="/struts-tags" %&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Numberguess&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;h2&gt;Numberguess Game - Struts 2 CDI Example&lt;/h2&gt;

&lt;h3&gt;I've picked a number between &lt;s:property value="game.smallest"/&gt; and &lt;s:property value="game.biggest"/&gt;.
    You have &lt;s:property value="game.remainingGuesses"/&gt;remaining guesses.&lt;/h3&gt;

&lt;s:form action="guess"&gt;
    &lt;s:textfield name="game.guess" label="Your Guess"/&gt;
    &lt;s:submit/&gt;
&lt;/s:form&gt;
&lt;p/&gt;
&lt;s:actionerror/&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>
</div></div>

<p>along - if not using the fabulous and recommended Struts 2 <a shape="rect" href="convention-plugin.html">Convention Plugin</a> - with a struts.xml like this</p>

<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: xml; gutter: false; theme: Default" style="font-size:12px;">
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE struts PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"       
          "http://struts.apache.org/dtds/struts-2.3.dtd"&gt;
&lt;struts&gt;
  &lt;constant name="struts.enable.DynamicMethodInvocation" value="false"/&gt;
  &lt;constant name="struts.devMode" value="true"/&gt;
  
    &lt;package name="numberguess" extends="struts-default"&gt;

        &lt;action name="NumberGuess" class="org.apache.struts2.example.cdi.NumberGuess"&gt;
            &lt;result&gt;/WEB-INF/pages/NumberGuess.jsp&lt;/result&gt;
        &lt;/action&gt;

        &lt;action name="guess" class="org.apache.struts2.example.cdi.NumberGuess" method="guess"&gt;
            &lt;result&gt;/WEB-INF/pages/NumberGuess.jsp&lt;/result&gt;
        &lt;/action&gt;

    &lt;/package&gt;

&lt;/struts&gt;
</pre>
</div></div>

<p>Now you can add the business logic we want to be managed and injected by CDI. Start with two qualifier annotations:</p>


<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
package org.apache.struts2.example.cdi;

import javax.inject.Qualifier;
import java.lang.annotation.Documented;
import static java.lang.annotation.ElementType.*;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Target;

@Target( { TYPE, METHOD, PARAMETER, FIELD })
@Retention(RUNTIME)
@Documented
@Qualifier
public @interface Random {}
</pre>
</div></div>

<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
package org.apache.struts2.example.cdi;

import javax.inject.Qualifier;
import java.lang.annotation.Documented;
import static java.lang.annotation.ElementType.*;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Target;

@Target( { TYPE, METHOD, PARAMETER, FIELD })
@Retention(RUNTIME)
@Documented
@Qualifier
public @interface MaxNumber {}
</pre>
</div></div>

<p>Now on to the actual business beans, the Game and the Generator bean:</p>

<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
package org.apache.struts2.example.cdi;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;

@Named
@SessionScoped
public class Game implements Serializable {

    public static final String CORRECT = "Correct !!!";
    public static final String WRONG = "Sorry, wrong number !!!";

    private int number;
    private int guess;
    private int smallest;

    @MaxNumber
    @Inject
    private int maxNumber;

    private int biggest;
    private int remainingGuesses;

    @Random
    @Inject
    Instance&lt;Integer&gt; randomNumber;

    public Game() {
    }

    public int getNumber() {
        return number;
    }

    public int getGuess() {
        return guess;
    }

    public void setGuess( int guess ) {
        this.guess = guess;
    }

    public int getSmallest() {
        return smallest;
    }

    public int getBiggest() {
        return biggest;
    }

    public int getRemainingGuesses() {
        return remainingGuesses;
    }

    public String check() throws InterruptedException {
        if (guess &gt; number) {
            biggest = guess - 1;
        }
        if (guess &lt; number) {
            smallest = guess + 1;
        }
        if (guess == number) {
            return CORRECT;
        }
        remainingGuesses--;
        return WRONG;
    }

    @PostConstruct
    public void reset() {
        this.smallest = 0;
        this.guess = 0;
        this.remainingGuesses = 10;
        this.biggest = maxNumber;
        this.number = randomNumber.get();
    }

}
</pre>
</div></div>

<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
package org.apache.struts2.example.cdi;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import java.io.Serializable;

@ApplicationScoped
public class Generator implements Serializable {
   
   private java.util.Random random = new java.util.Random( System.currentTimeMillis() );
   
   private int maxNumber = 100;
   
   java.util.Random getRandom() {
      return random;
   }
   
   @Produces @Random int next() { 
      return getRandom().nextInt(maxNumber); 
   }
   
   @Produces @MaxNumber int getMaxNumber() {
      return maxNumber;
   }

} 
</pre>
</div></div>

<p>If you understand that code at a glance, you are either already an CDI expert or profit from the readable, natural language oriented way the CDI stack works. If neither of this is the case, now it's time to check the CDI and Weld documentation. Remember, this is a trivial example - there is much more to know about CDI.</p>

<p>Ready you are now - use the Force!</p></div>
        </div>

        
    </div>
</div>
<div class="footer">
    Generated by CXF SiteExporter
</div>
</body>
</html>
